package lesson08;

import java6703.lesson08.Course6703;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ListSort6703ViewController {
    @FXML
    private ListView<java6703.lesson08.Course6703> lvScore;
    @FXML
    private ToggleGroup lvRadios;

    public ObservableList<java6703.lesson08.Course6703> items;
    public List<java6703.lesson08.Course6703> courseList = Arrays.asList(
            new java6703.lesson08.Course6703(80, "1上", 4, "A001", "C程序设计基础"),
            new java6703.lesson08.Course6703(80, "1上", 3, "0001", "大学英语"),
            new java6703.lesson08.Course6703(85, "1下", 2, "B002", "Pthon程序基础"),
            new java6703.lesson08.Course6703(85, "1下", 2, "0002", "高等数学"),
            new java6703.lesson08.Course6703(78, "2上", 3, "A002", "面向对象程序设计"),
            new java6703.lesson08.Course6703(78, "2上", 3, "A003", "数据库原理"),
            new java6703.lesson08.Course6703(90, "2下", 3, "B001", "操作系统"),
            new java6703.lesson08.Course6703(75, "3上", 3, "B003", "网络基础"),
            new java6703.lesson08.Course6703(83, "3上", 2, "C001", "Java程序高级开发"),
            new java6703.lesson08.Course6703(75, "3下", 3, "C002", "软件工程与UML")
    );

    @FXML
    void initialize() {

        items = lvScore.getItems();
        items.addAll(courseList);
        scoreSort();

        lvRadios.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
            String selectBtn = ((RadioButton) newValue).getText();

            switch (selectBtn) {

                case "按学期升序、成绩降序":
                    termCreditSort1();
                    break;

                case "按成绩降序":
                    scoreSort();
                    break;

                case "按学期升序":
                    termSort();
                    break;

                case "按学分升序":
                    creditSort();
                    break;

                case "按学期升序、学分降序":
                    termCreditSort2();
                    break;

                default:
                    break;
            }
        });
    }

    void termCreditSort1() {
        courseList.sort(Comparator.comparing(java6703.lesson08.Course6703::getTerm).reversed().
                thenComparing(java6703.lesson08.Course6703::getScore).reversed());
        items.setAll(courseList);
    }

    void scoreSort() {
        courseList.sort(Comparator.comparingInt(java6703.lesson08.Course6703::getScore).reversed());
        items.setAll(courseList);
    }

    void termSort() {
        courseList.sort(Comparator.comparing(java6703.lesson08.Course6703::getTerm));
        items.setAll(courseList);
    }

    void creditSort() {
        courseList.sort(((o1, o2) -> o1.getCredit() - o2.getCredit()));
        items.setAll(courseList);
    }

    void termCreditSort2() {
        courseList.sort(Comparator.comparing(java6703.lesson08.Course6703::getTerm).reversed().
                thenComparing(Course6703::getCredit).reversed());
        items.setAll(courseList);
    }
}